package com.bluesea.report.service.customer.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.bluesea.report.dao.CustomerDao;
import com.bluesea.report.dao.CustomerDetailDao;
import com.bluesea.report.entity.Customer;
import com.bluesea.report.entity.CustomerDetail;
import com.bluesea.report.service.customer.CustomerService;


/**
 * CustomerService服务层实现类
* @ClassName: CustomerServiceImpl
* @Description: 增删改查
* @author 李爽
* @date 2017年8月15日 下午4:38:31
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService{

	@Resource
	private CustomerDao dao; //将客户表的dao类注入
	
	@Resource
	private CustomerDetailDao custDdao; //客户明细表daodetail注入
	
	public List<Customer> getAll() { //查
		List<Customer> list = dao.getAll();//调dao里的查询所有的方法
		return list;
	}


	/**
	 * @Description: 通过json先将map转成字符串，再通过json将字符串转成对象
	 * 				 明细表的创建方法会返回自增的id，将他设置到客户表的detailId属性中去
	 * @param @param map
	 * @param @return    设定文件
	 */
	public Integer create(Map<String, Object> map) { //增
		Customer cust = JSON.parseObject(JSON.toJSONString(map),Customer.class);
		CustomerDetail custd = JSON.parseObject(JSON.toJSONString(map),CustomerDetail.class);
		Integer j = custDdao.createAndGetId(custd);
		cust.setDetailId(custd.getId());
		Integer i =dao.create(cust);//调dao里的增加方法
		return i+j;
	}
	
	/**
	 * @Description: 先从客户表的detailId属性中获取明细表的id，再根据id进行修改
	 * @param @param map    设定文件
	 */
	public void modify(Map<String, Object> map) {
		Customer cust = JSON.parseObject(JSON.toJSONString(map),Customer.class);
		CustomerDetail custd = JSON.parseObject(JSON.toJSONString(map),CustomerDetail.class);
		custd.setId(cust.getDetailId());
		dao.modify(cust);
		custDdao.modifyCustomerDetail(custd);
		
	}

	public Integer remove(Integer id) { //删
		Integer cremove = dao.remove(id); //调dao里的删除方法
		return cremove;
	}

		public int deleteBatch(Integer[] ids) {
		Integer deleteBatch=dao.deleteBatch(ids);
		return deleteBatch; //调用dao里的批量删除
	}

	public List<Customer> search(Customer cust) {
		List<Customer> customers=dao.search(cust); //调用dao持久化 里的条件查询
		return customers;
	}

	/**
	 * 批量撤户
	 */
	public void invalid(Integer[] id) {
		dao.invalid(id);
	}

	/**
	 * 关联查询
	 */
	public List<Object> queryAll() {
		return dao.queryAll();
	}

}
